<?php


/**
 * 异步实时刷新商品竞拍属性
 * auction.js  ajaxGetVal
 * Enter description here ...
 * @var unknown_type
 */
define('ROOT_PATH', str_replace("\\", '/', dirname(__FILE__)));
$config = include(ROOT_PATH.'/Lib/common.ini.php');


	$data = Init_GP(array('id','_v'));   //商品竞拍ID
	$id = explode(',',$data['id']);
	
	//TODO:初始0，后序传入时间数字
	if ($data['_v'] == 0){
		$_d = getAllData($id);  //获取全部数据
	}else {
		$_d = getChangeData($id,$data['_v']);  //获取变动数据
	}
	
	$json = array(
		'_v'=>microtime(true),
		'_d'=>$_d
	);
	
	$json = json_encode($json);
	echo $json;

	
	
	
	

/**
 * 获取全部数据
 * Enter description here ...
 * @param unknown_type $id
 */
function getAllData($id){
	global $get;
	
	foreach ($id as $val){
		$val = (int)$val;
		$info = format($get->getAuctionData($val));
		if($info){
			$data[] = $info;
		}
	}
	if (empty($data)){
		$data = array();
	}
	return $data;
}




/**
 * 获取变动数据
 * Enter description here ...
 * @param unknown_type $id
 * @param unknown_type $time
 * @return multitype:
 */
function getChangeData($id,$time){
	global $get,$cache;
	$n = 0;
	while (change($id,$time,$data) && $n < 5){
		sleep(1);
		$n++;
	}
	
	if ($n >= 5){
		$data=getAllData($id);
	}

	return $data;
}

/**
 * 
 * Enter description here ...
 * @param unknown_type $id
 * @param unknown_type $time
 * @param unknown_type $data
 */
function change($id,$time,&$data){
	global $get;
	foreach ($id as $val){
		$tmp = $get->getAuctionData($val);
		//更新过的 或竞拍结束的
		if ($tmp['uptime'] > $time || $tmp['endtime'] < time()){
			$info = format($get->getAuctionData($val));
			if($info){
				$data[] = $info;
			}
		}
		unset($tmp);
	}
	if (empty($data)){
		return true;
	}
	return false;
	
}



/**
 * 格式化返回数据
 * Enter description here ...
 * @param unknown_type $data
 */
function format($data){
	global $cache;
	$time = time();
	if(!empty($data)){
		if($data['state'] == 1){
			if($data['delaytype'] != 0){
				$data = delayaction($data);
			}
			if ($data['endtime'] < $time){
				//格式化数据
				$data = set_state($data['id']);
				$value = array(
					'id'=>$data['id'],
					'end'=>0,   //结束秒数
					'price'=>$data['realprice'],
					'state'=>0,   //状态
					'name'=>$data['auctionuname'],
					'uid'=>$data['uid'],
					'head'=>$data['head']
				);
				
			}else {
				//格式化数据
				$value = array(
					'id'=>$data['id'],
					'end'=>$data['endtime']-$time,   //结束秒数
					'price'=>$data['realprice'],
					'state'=>$data['state'],   //状态
					'name'=>$data['auctionuname'],
					'uid'=>$data['uid'],
					'head'=>$data['head']
				);
			}
		}else{
			//格式化数据
			$value = array(
				'id'=>$data['id'],
				'end'=>$data['endtime']-$time,   //结束秒数
				'price'=>$data['realprice'],
				'state'=>3,   //状态
				'name'=>$data['auctionuname'],
				'uid'=>$data['uid'],
				'head'=>$data['head']
			);
			$cache->rm($data['id']);
		}
	}else{
		return false;
	}
	return $value;
}




/**
 * 更改竞拍状态
 * Enter description here ...
 * @param unknown_type $id
 */
function set_state($id){
	global $db,$prefix,$cache;
	$sql = "UPDATE `{$prefix}auction` SET `state` = '2' WHERE `id` = {$id}";
	$db->query($sql);
	$cache->rm($id);
}


/**
 * 竞拍延时
 * Enter description here ...
 * @param unknown_type $value
 */
function delayaction($value){
	global $db,$prefix,$sysconfig,$cache;

	$mauction_time = $sysconfig['mauction_time'];    //早间竞拍延时开始时间（24小时点数）
	$eauction_time = $sysconfig['eauction_time'];    //晚间竞拍延时开始时间（24小时点数）
	$now = time();
	$nowh = date('H',$now);

	
	if($nowh<9 && $value['delaytype'] == 1 && $nowh==$mauction_time){
		$endtime = $now+(9-$mauction_time)*60*60;
		if($value['endtime'] < $endtime){
		    $time = $endtime + rand(60,3540);
		    $sql = "UPDATE `{$prefix}auction` SET `endtime` = '{$time}' WHERE `id` = {$value['id']}";
			$db->query($sql);
			unset($sql);
			$value['endtime'] = $time;
			$cache->set($value['id'],$value);
		}
	}
	
	
	if($nowh>10 && $nowh<21 && $value['delaytype'] == 2 && $nowh==$eauction_time){
	   $endtime = $now+(21-$eauction_time)*60*60+60;
	   if($value['endtime'] < $endtime){
		    $time = $endtime + rand(60,3540);
		    $sql = "UPDATE `{$prefix}auction` SET `endtime` = '{$time}' WHERE `id` = {$value['id']}";
			$db->query($sql);
			unset($sql);
			$value['endtime'] = $time;
			$cache->set($value['id'],$value);
		}
	}
	
	return $value;
}



?>